package ynu.sxp.demoapp.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ynu.sxp.demoapp.entity.User;
import ynu.sxp.demoapp.service.UserService;
import ynu.sxp.demoapp.common.Result;

@RestController
@RequestMapping("/UserController")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/getUserByIdByPass")
    public Result<User> getUserByIdByPass(@RequestParam String userId, @RequestParam String password) {
        User user = userService.getUserByIdAndPassword(userId, password);
        return Result.success(user);
    }

    @GetMapping("/getUserById")
    public Result<User> getUserById(@RequestParam String userId) {
        User user = userService.getUserById(userId);
        return Result.success(user);
    }

    @PostMapping("/saveUser")
    public Result<Void> saveUser(@RequestBody User user) {
        try {
            userService.saveUser(user);
            return Result.success(null);
        } catch (Exception e) {
            return Result.error(500, "注册失败：" + e.getMessage());
        }
    }
}